package ru.compft.app.as_db.ws;

import org.springframework.util.CollectionUtils;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import ru.compft.app.as_db.business.BusinessFacade;
import ru.compft.app.as_db.ws.params.UserForWebService;
import ru.compft.app.as_db.ws.params.UserWebServiceRequest;
import ru.compft.app.as_db.ws.params.UserWebServiceResponse;
import ru.compft.common.data.AppRole;
import ru.compft.common.data.AppUser;
import ru.compft.common.exception.PermissionDeniedException;
import ru.compft.common.services.UserService;

import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: VVasiliev
 * Date: 31.10.12
 * Time: 14:07
 */
@WebService(serviceName = "UserWebService")
public class UserServiceEndpoint extends SpringBeanAutowiringSupport {

    @Resource(name = "userService")
    private UserService userService;

    @Resource(name = "appBusinessDelegate")
    private BusinessFacade appBusinessDelegate;

    @WebMethod
    public String pingService() {
        return "UserWebService say 'Hello World'";
    }

    @WebMethod
    @WebResult
    public UserWebServiceResponse getUserList(UserWebServiceRequest request) {
        // аутентифицируем пользователя
        final AppUser user = userService.authentication(request.getLogin(), request.getPassword());

        // выполняем бизнес метод
        try {
            final List<AppUser> appUserList = appBusinessDelegate.showAllUsers();

            final List<UserForWebService> userForWebServices = new ArrayList<UserForWebService>();

            if (!CollectionUtils.isEmpty(appUserList)) {
                for (AppUser appUser : appUserList) {
                    final UserForWebService forWebService = new UserForWebService();

                    forWebService.setLogin(appUser.getLogin());
                    forWebService.setFirstName(appUser.getFirstName());
                    forWebService.setLastName(appUser.getLastName());
                    forWebService.setEnabled(appUser.isEnabled());
                    forWebService.setAccountNonExpired(appUser.isAccountNonExpired());
                    forWebService.setAccountNonLocked(appUser.isAccountNonLocked());
                    forWebService.setCredentialsNonExpired(appUser.isCredentialsNonExpired());
                    forWebService.setRegDate(appUser.getRegDate());
                    forWebService.setLastActivity(appUser.getLastActivity());

                    final Set<AppRole> roles = appUser.getRoles();
                    if (!CollectionUtils.isEmpty(roles)) {
                        List<String> roleList = new ArrayList<String>();
                        for (AppRole role : roles) {
                            roleList.add(role.getName());
                        }

                        forWebService.setRoles(roleList);
                    }
                    userForWebServices.add(forWebService);
                }
            }

            final UserWebServiceResponse response = new UserWebServiceResponse();
            response.setUserList(userForWebServices);

            return response;
        } catch (PermissionDeniedException e) {
            // пишем ошибку
            e.printStackTrace();
        }
        return null;
    }
}
